using KL.Core.Common.infrastructure;
using KL.Core.Common.MyOptions;
using KL.Core.Common.Utils;
using KL.Core.Extensions.HostedService;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Minio;

namespace KL.Core.Extensions.MinIO;

public static class MinIOSetup
{
    /// <summary>
    /// MinIO 设置
    /// </summary>
    /// <param name="services"></param>
    /// <exception cref="ArgumentNullException"></exception>
    public static void AddMinIOSetup(this IServiceCollection services,ConfigurationManager Configuration)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        var minIoOptions = Configuration.GetSection("MinIO").Get<MinIOOptions>(); 
        //var flag = AppSettings.app(new string[] { "MinIO", "Enabled" });
        if (minIoOptions?.Enabled??false)
        {
            
            // Add Minio using the default endpoint
            //services.AddMinio(minIoOptions.Connection.USERID, minIoOptions.Connection.PWD);
            var endpoint = $"{minIoOptions.Connection.IP}:{minIoOptions.Connection.PORT}";
            // Add Minio using the custom endpoint and configure additional settings for default MinioClient initialization
            services.AddMinio(configureClient => configureClient
                .WithEndpoint(endpoint)
                .WithCredentials(minIoOptions.Connection.USERID, minIoOptions.Connection.PWD)
                .WithSSL(false)
                .Build());

            // NOTE: SSL and Build are called by the build-in services already
            //services.AddSingleton<IHostedService, MinIOConcumerService>();
        }
    }
}